package com.njym.java.security.app.rest.client;



import com.alibaba.fastjson.JSONObject;
import com.njym.java.security.app.biz.FamilyNewsBiz;
import com.njym.java.security.app.biz.UserMessageBiz;
import com.njym.java.security.app.entity.FamilyNews;
import com.njym.java.security.app.entity.GroupsMsgContent;
import com.njym.java.security.app.entity.UserMessage;
import com.njym.java.security.app.entity.UserTeacherMessage;
import com.njym.java.security.common.biz.BaseBiz;
import com.njym.java.security.common.rest.BaseController;
import org.jim.common.Protocol;
import org.jim.common.packets.Command;
import org.jim.common.tcp.TcpPacket;
import org.jim.common.tcp.TcpServerDecoder;
import org.jim.common.tcp.TcpServerEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.tio.client.intf.ClientAioHandler;
import org.tio.core.ChannelContext;
import org.tio.core.GroupContext;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.intf.AioHandler;
import org.tio.core.intf.Packet;

import java.nio.ByteBuffer;
import java.util.Date;

/**
 *
 * 版本: [1.0]
 * 功能说明:
 * 作者: WChao 创建时间: 2017年8月30日 下午1:10:28
 */
public class HelloClientAioHandler implements AioHandler,ClientAioHandler{
	private static TcpPacket heartbeatPacket = new TcpPacket(Command.COMMAND_HEARTBEAT_REQ, new byte[]{Protocol.HEARTBEAT_BYTE});


	public static UserMessageBiz biz;

	@Override
	public Packet heartbeatPacket() {
		return heartbeatPacket;
	}

	@Override
	public Packet decode(ByteBuffer byteBuffer, ChannelContext channelContext) throws AioDecodeException {
		TcpPacket tcpPacket = TcpServerDecoder.decode(byteBuffer, channelContext);
		return tcpPacket;
	}

	@Override
	public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) {
		TcpPacket tcpPacket = (TcpPacket)packet;
		return TcpServerEncoder.encode(tcpPacket,groupContext,channelContext);
	}

	@Override
	public void handler(Packet packet, ChannelContext channelContext) throws Exception {
		System.out.println("经过");
		System.out.println(channelContext);
		TcpPacket helloPacket = (TcpPacket)packet;
		byte[] body = helloPacket.getBody();
		if(body != null){
			String str = new String(body, Const.CHARSET);
			System.out.println("收到消息"+str);

			JSONObject object = JSONObject.parseObject(str);
			if(object.getIntValue("command") ==(11)) {
				JSONObject objects = JSONObject.parseObject(object.getString("data"));
				String[] type = objects.getString("content").split(";");
				if(type[type.length -1].equals("friend")) {
					UserMessage userMessage = new UserMessage();
					if (!objects.getString("from").equals("hello_clientss")) {
						userMessage.setPostMessage(objects.getString("content"));
						userMessage.setDateTime(new Date());
						userMessage.setFromUserId(objects.getIntValue("from"));
						userMessage.setToUserId(objects.getIntValue("to"));
						userMessage.setAttr1(objects.getString("msgType"));
						userMessage.setAttr2(objects.getString("from") + objects.getString("to"));
						biz.insertMessage(userMessage);
					}
				}
				if(type[type.length -1].equals("group")) {
					GroupsMsgContent groupsMsgContent = new GroupsMsgContent();
					if (!objects.getString("from").equals("hello_clientss")) {
						groupsMsgContent.setContent(objects.getString("content"));
						groupsMsgContent.setCreateTime(new Date());
						groupsMsgContent.setFromId(objects.getIntValue("from"));
						groupsMsgContent.setAttr2(objects.getString("to"));
						groupsMsgContent.setAttr1(objects.getString("msgType"));

						biz.insertGroupMessage(groupsMsgContent);
					}
				}
				if(type[type.length -1].equals("teacher")) {
					UserTeacherMessage userMessage = new UserTeacherMessage();
					if (!objects.getString("from").equals("hello_clientss")) {
						userMessage.setPostMessage(objects.getString("content"));
						userMessage.setDateTime(new Date());
						userMessage.setFromUserId(objects.getIntValue("from"));
						userMessage.setToUserId(objects.getIntValue("to"));
						userMessage.setAttr1(objects.getString("msgType"));
						userMessage.setAttr2(objects.getString("from") + objects.getString("to"));
						biz.insertTeacherMessage(userMessage);
					}
				}
			}
		}
	}
}
